<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .wrapper {
        width: 400px;
        height: 80px;
        background-color: orange;
        position: absolute;
        left: -400px;
        top: 200px;
      }
      .wrapper span {
        width: 50px;
        height: 80px;
        background-color: red;
        position: absolute;
        right: -50px;
        top: 0;
      }
    </style>
  </head>
  <body>
    <div class="wrapper">
      <span></span>
    </div>

    <script>
      const wrapperEl = document.querySelector(".wrapper");
      let timer = null;
      wrapperEl.onmouseenter = function () {
        startMove(this, 0);
      };

      wrapperEl.onmouseleave = function () {
        startMove(this, -400);
      };

      function getStyle(dom, attr) {
        if (window.getComputedStyle) {
          return window.getComputedStyle(dom)[attr];
        } else {
          return dom.currentStyle[attr];
        }
      }

      function startMove(dom, target) {
        clearInterval(timer);
        let iSpeed = null;
        timer = setInterval(function () {
          iSpeed = (target - dom.offsetLeft) / 7;
          iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
          if (dom.offsetLeft === target) {
            clearInterval(timer);
          } else {
            dom.style.left = dom.offsetLeft + iSpeed + "px";
          }
        }, 30);
      }
    </script>
  </body>
</html>
